package com.ruoyi.web.controller.system;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.domain.imp.TypeImport;
import com.ruoyi.system.domain.vo.TypeVO;
import com.ruoyi.system.domain.vo.Type_mini;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.service.ITypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 分类管理Controller
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@RestController
@RequestMapping("/system/type")
public class TypeController extends BaseController {
    @Resource
    private ITypeService typeService;

    /**
     * 查询分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(Type type) {
        if (Convert.toInt(ServletUtils.getParameter("pageNum")) != -1
                && Convert.toInt(ServletUtils.getParameter("pageSize")) != -1) {
            startPage();
            List<Type> list = typeService.selectTypeList(type);
            return getDataTable(list);
        } else {
            List<Type_mini> typeMinis = typeService.selectTypeList_mini(type);
            return getDataTable(typeMinis);
        }
    }

    /**
     * 导出分类管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Type type) {
        List<Type> list = typeService.selectTypeList(type);
        ExcelUtil<Type> util = new ExcelUtil<Type>(Type.class);
        util.exportExcel(response, list, "分类管理数据");
    }

    //@PreAuthorize("@ss.hasPermi('system:type:import')")
    @Log(title = "分类管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TypeImport> excelUtil = new ExcelUtil<>(TypeImport.class);
        List<TypeImport> list = excelUtil.importExcel(file.getInputStream());
        typeService.importType(list);
        return AjaxResult.success();
    }

    /**
     * 获取分类管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(typeService.selectTypeById(id));
    }

    /**
     * 新增分类管理
     */
    //@PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Type type) {
        return toAjax(typeService.insertType(type));
    }

    /**
     * 修改分类管理
     */
    //@PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Type type) {
        return toAjax(typeService.updateType(type));
    }

    /**
     * 删除分类管理
     */
    //@PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(typeService.deleteTypeByIds(ids));
    }
}
